Clover icon

compiler

  1. Project Clover database Mon Jan 2 2023 15:09:37 MST
  2. Package com.google.javascript.rhino.jstype

File CanCastToVisitor.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

26
48
15
1
182
111
34
0.71
3.2
15
2.27

Classes

Class Line # Actions
CanCastToVisitor 45 48 34 2
0.977528197.8%
 

Contributing tests

This file is covered by 5080 tests. .

Source view

1    /*
2    *
3    * ***** BEGIN LICENSE BLOCK *****
4    * Version: MPL 1.1/GPL 2.0
5    *
6    * The contents of this file are subject to the Mozilla Public License Version
7    * 1.1 (the "License"); you may not use this file except in compliance with
8    * the License. You may obtain a copy of the License at
9    * http://www.mozilla.org/MPL/
10    *
11    * Software distributed under the License is distributed on an "AS IS" basis,
12    * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13    * for the specific language governing rights and limitations under the
14    * License.
15    *
16    * The Original Code is Rhino code, released
17    * May 6, 1999.
18    *
19    * The Initial Developer of the Original Code is
20    * Netscape Communications Corporation.
21    * Portions created by the Initial Developer are Copyright (C) 1997-1999
22    * the Initial Developer. All Rights Reserved.
23    *
24    * Contributor(s):
25    * John Lenz
26    * Google Inc.
27    *
28    * Alternatively, the contents of this file may be used under the terms of
29    * the GNU General Public License Version 2 or later (the "GPL"), in which
30    * case the provisions of the GPL are applicable instead of those above. If
31    * you wish to allow use of your version of this file only under the terms of
32    * the GPL and not to allow others to use your version of this file under the
33    * MPL, indicate your decision by deleting the provisions above and replacing
34    * them with the notice and other provisions required by the GPL. If you do
35    * not delete the provisions above, a recipient may use your version of this
36    * file under either the MPL or the GPL.
37    *
38    * ***** END LICENSE BLOCK ***** */
39   
40    package com.google.javascript.rhino.jstype;
41   
42    /**
43    * A "can cast to" relationship visitor.
44    */
 
45    class CanCastToVisitor implements RelationshipVisitor<Boolean> {
46   
 
47  30517 toggle @Override
48    public Boolean caseUnknownType(JSType thisType, JSType thatType) {
49  30517 return true;
50    }
51   
 
52  2394 toggle @Override
53    public Boolean caseNoType(JSType thatType) {
54  2394 return true;
55    }
56   
 
57  2558 toggle @Override
58    public Boolean caseNoObjectType(JSType thatType) {
59  2558 return true; // TODO(johnlenz): restrict to objects
60    }
61   
 
62  2100 toggle @Override
63    public Boolean caseAllType(JSType thatType) {
64  2100 return true;
65    }
66   
 
67  1477 toggle boolean canCastToUnion(JSType thisType, UnionType unionType) {
68  1477 for (JSType type : unionType.getAlternates()) {
69  1647 if (thisType.visit(this, type)) {
70  1449 return true;
71    }
72    }
73  28 return false;
74    }
75   
 
76  1274 toggle boolean canCastToFunction(JSType thisType, FunctionType functionType) {
77  1274 if (thisType.isFunctionType()) {
78    // TODO(johnlenz): visit function parts
79  928 return true;
80    } else {
81  346 return thisType.isSubtype(functionType)
82    || functionType.isSubtype(thisType);
83    }
84    }
85   
 
86  23720 toggle private boolean isInterface(JSType type) {
87  23720 ObjectType objType = type.toObjectType();
88  23720 if (objType != null) {
89  22431 JSType constructor = objType.getConstructor();
90  22431 return constructor != null && constructor.isInterface();
91    }
92  1289 return false;
93    }
94   
 
95  17336 toggle Boolean castCastToHelper(JSType thisType, JSType thatType) {
96  17336 if (thatType.isUnknownType()
97    || thatType.isAllType()
98    || thatType.isNoObjectType() // TODO(johnlenz): restrict to objects
99    || thatType.isNoType()) {
100  4558 return true;
101  12778 } else if (thisType.isRecordType() || thatType.isRecordType()) {
102  772 return true; // TODO(johnlenz): are there any misuses we can catch?
103  12006 } else if (isInterface(thisType) || isInterface(thatType)) {
104  501 return true; // TODO(johnlenz): are there any misuses we can catch?
105  11505 } else if (thatType.isEnumElementType()) {
106  58 return thisType.visit(this,
107    thatType.toMaybeEnumElementType().getPrimitiveType());
108  11447 } else if (thatType.isUnionType()) {
109  1477 return canCastToUnion(thisType, thatType.toMaybeUnionType());
110  9970 } else if (thatType.isFunctionType()) {
111  1274 return canCastToFunction(thisType, thatType.toMaybeFunctionType());
112  8696 } else if (thatType.isParameterizedType()) {
113    // TODO(johnlenz): once the templated type work is finished,
114    // restrict the type parameters.
115  1806 return thisType.visit(this,
116    thatType.toMaybeParameterizedType().getReferencedTypeInternal());
117    }
118   
119  6890 return thisType.isSubtype(thatType) || thatType.isSubtype(thisType);
120    }
121   
 
122  474 toggle @Override
123    public Boolean caseValueType(ValueType thisType, JSType thatType) {
124  474 return castCastToHelper(thisType, thatType);
125    }
126   
 
127  14920 toggle @Override
128    public Boolean caseObjectType(ObjectType thisType, JSType thatType) {
129  14920 return castCastToHelper(thisType, thatType);
130    }
131   
 
132  1942 toggle @Override
133    public Boolean caseFunctionType(FunctionType thisType, JSType thatType) {
134  1942 return castCastToHelper(thisType, thatType);
135    }
136   
 
137  2142 toggle @Override
138    public Boolean caseUnionType(UnionType thisType, JSType thatType) {
139  2142 boolean visited = false;
140  2142 for (JSType type : thisType.getAlternates()) {
141  2206 if (type.isVoidType() || type.isNullType()) {
142    // Don't allow if the only match between the types is null or void,
143    // otherwise any nullable type would be castable to any other nullable
144    // type and we don't want that.
145    } else {
146  2176 visited = true;
147  2176 if (type.visit(this, thatType)) {
148  2128 return true;
149    }
150    }
151    }
152   
153    // Special case the "null|undefined" union and allow it to be cast
154    // to any cast to any type containing allowing either null|undefined.
155  14 if (!visited) {
156  12 JSType NULL_TYPE = thisType.getNativeType(JSTypeNative.NULL_TYPE);
157  12 JSType VOID_TYPE = thisType.getNativeType(JSTypeNative.VOID_TYPE);
158  12 return NULL_TYPE.visit(this, thatType) || VOID_TYPE.visit(this, thatType);
159    }
160   
161  2 return false;
162    }
163   
 
164  2772 toggle @Override
165    public Boolean caseParameterizedType(
166    ParameterizedType thisType, JSType thatType) {
167    // TODO(johnlenz): once the templated type work is finished,
168    // restrict the type parameters.
169  2772 return thisType.getReferencedTypeInternal().visit(this, thatType);
170    }
171   
 
172  0 toggle @Override
173    public Boolean caseTemplateType(TemplateType thisType, JSType thatType) {
174  0 return true;
175    }
176   
 
177  108 toggle @Override
178    public Boolean caseEnumElementType(
179    EnumElementType typeType, JSType thatType) {
180  108 return typeType.getPrimitiveType().visit(this, thatType);
181    }
182    }